package lines.gameengine.util;

import lines.gameengine.Ball;
import lines.gameengine.FieldPoint;
import lines.gameengine.stub.StubField;
import org.junit.Before;
import org.junit.Test;

import java.util.Collection;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

/**
 * @author vpotapenko
 */
public class DefaultCellsChooserTest {

    private DefaultCellsChooser chooser;
    private StubField field;

    @Before
    public void setUp() throws Exception {
        chooser = new DefaultCellsChooser();
        field = new StubField(3, 3);
        field.context.parameters.setBallsPerStep(3);
    }

    @Test public void shouldChooseAllEmptyCells() {
        Collection<FieldPoint> points = chooser.chooseEmptyCellsForNextStep(field);
        fillPoints(points);

        points = chooser.chooseEmptyCellsForNextStep(field);
        fillPoints(points);

        points = chooser.chooseEmptyCellsForNextStep(field);
        fillPoints(points);

        points = chooser.chooseEmptyCellsForNextStep(field);
        assertThat(points.isEmpty(), equalTo(true));

        for (int x = 0; x < field.getWidth(); x++) {
            for (int y = 0; y < field.getHeight(); y++) {
                if (field.getCell(x, y).isEmpty()) {
                    fail("Field has empty cells");
                }
            }
        }
    }

    private void fillPoints(Collection<FieldPoint> points) {
        for (FieldPoint point : points) {
            field.getCell(point).setBall(new Ball(null));
        }
    }
}
